import * as Cesium from "cesium"
import "cesium/Build/Cesium/Widgets/widgets.css"
window.CESIUM_BASE_URL = './static/Cesium'
import BaiduImageryProvider from './imagery/provider/BaiduImageryProvider.js'
class FGIS {
  constructor(props) {
    const { el, token, ready, errorCallback } = props;
    this.options = {
      el,
      token
    }
    this.Cesium = Cesium
    try {
      this._init().then(() => {
        typeof ready === 'function' && ready()
      })
    } catch (error) {
      typeof errorCallback === 'function' && errorCallback(error)
    }
    
  }
  async _init () {
    if(this.options.token) {
      Cesium.Ion.defaultAccessToken = this.options.token
    }
    const viewer = new Cesium.Viewer('map', {
      infoBox: false, // 是否显示点击要素之后显示的信息
      animation: false, // 动画控件
      homeButton: false, // 是否显示home键
      geocoder: false, // 是否显示地名查找控件
      timeline: false, // 是否显示时间线控件
      fullScreenButton: false, // 是否全屏显示
      sceneModePicker: false, // 是否显示投影方式控件
      navigationInstructionsInitiallyVisible: false,
      navigationHelpButton: false, // 是否显示帮助信息控件
      orderIndependentTranslucency: false,
      shouldAnimate: true,
      scene3DOnly: false, // 每个几何实例将只能以3d渲染以节省GPU内存
      selectionIndicator: false, // 取消点击有绿框
      imageryProvider: false, // 不提供地图
      baseLayerPicker: false,
      baseLayer: Cesium.ImageryLayer.fromProviderAsync(
        Cesium.TileMapServiceImageryProvider.fromUrl(
          Cesium.buildModuleUrl('Assets/Textures/NaturalEarthII')
        )
      )
    })
    // 使用bigmap下载的瓦片
    const baiduImageryProvider = new BaiduImageryProvider({
      // url: 'http://127.0.0.1:3000/{z}/{x}/{y}.png'
      url: 'http://10.98.14.44/nfs/digitaltwins-fms-admin/tiles/{z}/{x}/{y}.png',
      crs: 'WGS84'
    })
    viewer.imageryLayers.addImageryProvider(baiduImageryProvider)

    // const imgLayer =  new Cesium.UrlTemplateImageryProvider({
    //   url: 'http://127.0.0.1:3000/{z}/{x}/{y}.png',
    //   layer: 'imgLayer',
    //   minimumLevel: 0,
    //   maximumLevel: 19
    // })
    // viewer.scene.imageryLayers.addImageryProvider(imgLayer)
    // 添加百度地图图层
    // const baiduImageryProvider = new BaiduImageryProvider({
    //   url: 'http://api0.map.bdimg.com/customimage/tile?&x={x}&y={y}&z={z}&udt=20210401&scale=1&ak=5xiWSWWYi3UsJbtaGiPlop6DKJxXsFFM&styles=5323d28e2a95b8aa5cae5c3f0e523e5b'
    // })
    // viewer.imageryLayers.addImageryProvider(baiduImageryProvider)
    // 添加高德地图图层
    // const gaodeImageryProvider = new Cesium.UrlTemplateImageryProvider({
    //   url: 'https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}'
    // })
    // viewer.imageryLayers.addImageryProvider(gaodeImageryProvider)
    // 添加ArcGis图层
    // const arcgisImageryProvider = await Cesium.ArcGisMapServerImageryProvider.fromUrl('https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer')
    // viewer.imageryLayers.addImageryProvider(arcgisImageryProvider)
    //隐藏cesium左下角logo
    viewer._cesiumWidget._creditContainer.style.display = 'none';
    viewer.scene.screenSpaceCameraController.minimumZoomDistance = 717;
    viewer.scene.screenSpaceCameraController.maximumZoomDistance = 240000;
    this.viewer = viewer
  }
  flyTo(options) {
    this.viewer.camera.flyTo(options)
  }
  flyToEntity (entity, options) {
    this.viewer.flyTo(entity, options)
  }
  drawEntity (params) {
    const { id, name, position, point, polyline, billboard, label } = params
    let entityParams = {
      id,
      name
    }
    if (point) {
      const { color, pixelSize, outlineColor, outlineWidth, maxDistance, ...reset } = point
      const distanceDisplayCondition = maxDistance ? new Cesium.DistanceDisplayCondition(0, maxDistance) : undefined
      entityParams.position = position
      entityParams.point = {
        color: color ? new Cesium.Color.fromCssColorString(color) : Cesium.Color.SKYBLUE,
        pixelSize: pixelSize ? pixelSize : 10,
        outlineColor: outlineColor ? new Cesium.Color.fromCssColorString(outlineColor) : Cesium.Color.YELLOW,
        outlineWidth: outlineWidth ? outlineWidth : 3,
        distanceDisplayCondition,
        ...reset
      }
    }
    if (polyline) {
      const { paths, color, width, ...reset } = polyline
      entityParams.polyline = {
        positions: paths,
        width: width || 5,
        material: new Cesium.PolylineOutlineMaterialProperty({
          color: color ? new Cesium.Color.fromCssColorString(color) : Cesium.Color.GOLD,
          outlineColor: Cesium.Color.BLACK,
          outlineWidth: 0.1
        }), // 包边后锯齿问题好点
        clampToGround: true,
        ...reset
      }
    }
    if (billboard) {
      const { maxDistance, ...reset } = billboard
      const distanceDisplayCondition = maxDistance ? new Cesium.DistanceDisplayCondition(0, maxDistance) : undefined
      entityParams.position = position
      entityParams.billboard = {
        distanceDisplayCondition,
        ...reset
      }
    }
    if (label) {
      const { text, font, fillColor, scale, maxDistance, ...reset } = label
      const distanceDisplayCondition = maxDistance ? new Cesium.DistanceDisplayCondition(0, maxDistance) : undefined
      entityParams.position = position
      entityParams.label = {
        text: text,
        font: font ? font : '14pt Source Han Sans CN',
        fillColor: fillColor ? new Cesium.Color.fromCssColorString(fillColor) : Cesium.Color.WHITE,
        scale: scale ? scale : 1,
        distanceDisplayCondition,
        ...reset
      }
    }
    return this.viewer.entities.add(entityParams)
  }
  getEntityById (id) {
    return this.viewer.entities.getById(id)
  }
  getView () {
    const { positionWC, heading, pitch, roll } = this.viewer.camera
    return {
      destination: {
        x: positionWC.x,
        y: positionWC.y,
        z: positionWC.z
      },
      orientation: {
        heading,
        pitch,
        roll
      }
    }
  }
  destory() {
    this.viewer.entities.removeAll()
    this.viewer = null
    this.options = null
    this.Cesium = null
  }
}

export default FGIS